// pages/commodity_coupon/commodity_coupon.js
var app = getApp();
var ApiUrl = app.globalData.ApiUrl;
Page({

  /**
   * 页面的初始数据
   */
  data: {
    help_goods_voucher_points: '',
    my_uid: false,
    aaaaaa: '',
    bbbbbb: '',
    goods_voucher_info:null,
    goods_voucher_task: null,
    share_bimg:{}
  },
  //商品券信息详情
  //index.php?act=other_member&op=goods_voucher_info
  goods_voucher_info(goods_id){
    let that = this;
    let k = wx.getStorageSync('key');
    wx.request({
      url: ApiUrl + '/index.php?act=other_member&op=goods_voucher_info',
      data: {
        act: 'other_member',
        op: 'goods_voucher_info',
        key: k,
        goods_id: goods_id
      },
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      method: 'POST',
      success: function (result) {
        // console.log('商品券信息详情:==>', result);
        if (result.statusCode == 200) {
          let res = result.data.datas.info;
          if (result.data.code == 200) {
            // console.log('商品券信息详情:==>', res);
            that.setData({
              goods_voucher_info: res
            })
          } else {
            console.log('失败的时候商品券信息详情:==>', res);

          }
        }
      }
    });
  },
  //商品券-任务完成领取商品券
  //index.php?act=other_member&op=get_voucher_
  get_voucher_fn(){
    let that = this;
    let k = wx.getStorageSync('key');
    wx.request({
      url: ApiUrl + '/index.php?act=other_member&op=get_voucher_',
      data: {
        key: k,
        task_id: that.data.goods_voucher_task.task.id
      },
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      method: 'POST',
      success: function (result) {
        console.log('商品券-任务完成领取商品券:==>', result);
        if (result.statusCode == 200) {
          let res = result.data.datas;
          if (result.data.code == 200) {
            console.log('商品券-任务完成领取商品券:==>', res);
            // that.setData({
            //   get_gift_voucher_list: res
            // })
          } else {
            console.log('失败的时候领取商品券:==>', res);

          }
        }
      }
    });

  },
  //商品券-任务查看,没有任务领取
  //index.php?act=other_member&op=goods_voucher_task
  goods_voucher_task(goods_id) {
    let that = this;
    let k = wx.getStorageSync('key');
    wx.request({
      url: ApiUrl + '/index.php?act=other_member&op=goods_voucher_task',
      data: {
        act: 'other_member',
        op: 'goods_voucher_task',
        key: k,
        goods_id: goods_id
      },
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      method: 'POST',
      success: function (result) {
        console.log('商品券-任务查看:==>', result);
        if (result.statusCode == 200) {
          let res = result.data.datas.data;
          if (result.data.code == 200) {
            console.log('商品券-任务查看:==>', res);
            that.setData({
              goods_voucher_task: res
            })
            that.get_voucher_fn();
          } else {
            console.log('失败的时候商品券-任务查看:==>', res);

          }
        }
      }
    });
  },
  //商品券-助力的时候查看任务
  //index.php?act=other_member&op=see_task
  /** 
   * id任务的任务id
   */
  get_task_info(id) {
    let that = this;
    let k = wx.getStorageSync('key');
    wx.request({
      url: ApiUrl + '/index.php?act=other_member&op=see_task',
      data: {
        key: k,
        id: id
      },
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      method: 'POST',
      success: function (result) {
        if (result.statusCode == 200) {
          let res = result.data.datas;
          if (result.data.code == 200) {
            that.setData({
              goods_voucher_task: res,
              goods_voucher_info:res.goods_info
            })
            that.get_voucher_fn();
          } else {
            console.log('失败的时候商品券-任务查看:==>', res);

          }
        }
      }
    });
  },
  //index.php?act=other_member&op=help_goods_voucher
  //商品券助力
  help_goods_voucher() {
    let that = this;
    let k = wx.getStorageSync('key');
    let id = wx.getStorageSync('id');

    wx.request({
      url: ApiUrl + 'index.php?act=other_member&op=help_goods_voucher',
      data: {
        act: 'other_member',
        op: 'help_goods_voucher',
        key: k,
        uid: id
      },
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      method: 'POST',
      success: function (result) {
        console.log('商品券助力:==>', result);
        if (result.statusCode == 200) {
          if (result.data.code == 200) {
            console.log(result.data.datas);
            // that.deductionContent();
            let res = result.data.datas;
            that.setData({
              help_goods_voucher_points: res,
              my_uid: false,
              aaaaaa: k,
              bbbbbb: id
            })

          }
        }
      }
    });
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let that = this;
    if (options.id) {//邀请信息
      wx.setStorageSync('id', options.id);
      let goods_id = wx.getStorageSync('goods_id');

      this.get_task_info(options.id);
      that.setData({
        my_uid: true
      })
    }
    if (options.goods_id) {
      wx.setStorageSync('goods_id', options.goods_id);
      this.goods_voucher_info(options.goods_id);
      this.goods_voucher_task(options.goods_id);
    } 

    this.share_bimg();
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    // this.goods_voucher_task();
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },
  //index.php?act=other&op=share_bimg
  //背景图设置
  share_bimg() {
    let that = this;
    wx.request({
      url: ApiUrl + '/index.php?act=other&op=share_bimg',
      data: {
      },
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      success: function (result) {
        // console.log('背景图设置:==>', result);
        if (result.statusCode == 200) {
          if (result.data.code == 200) {
            let res = result.data.datas;
            // console.log('背景图设置:==>', res);
            that.setData({
              share_bimg: res
            })
          }
        }
      }
    });
  },
  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function (ops) {
    let id = this.data.goods_voucher_task.task.id;
    if (ops.from === 'button') {
      // 来自页面内转发按钮
      console.log(ops.target)
    }
    return {
      title: '快来！好友助力，快速得商品券！',
      imageUrl: '/image/WechatIMG91.jpeg',//图片地址
      path: '/pages/commodity_coupon/commodity_coupon?id=' + id+'&&type=goods_voucher',// 用户点击首先进入的当前页面
      success: function (res) {
        // 转发成功
        console.log("转发成功:");
        alert(1);
      },
      fail: function (res) {
        // 转发失败
        console.log("转发失败:");
      }
    }

  }
})